// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/base/api/EmergencyPlanApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/base/model/QueryEmergencyPlanListReq'
import OneClass from '@api/base/model/QueryEmergencyPlanDetailReq'
import AddClass from '@api/base/model/InsertEmergencyPlanReq'
import EditClass from '@api/base/model/UpdateEmergencyPlanReq'
import DelClass from '@api/base/model/DeleteEmergencyPlanReq'
import BaseClass from '@api/base/model/EmergencyPlanDto'
import GroupClass from '@api/base/model/EmergencyPlanGroupDto'
import FileClass from '@api/base/model/EmergencyPlanFileDto'
import StepClass from '@api/base/model/EmergencyPlanStepDto'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const BASE = [BaseClass, 'emergencyPlanDto']
const GROUP = [GroupClass, 'emergencyPlanGroupDto', 1]
const FILE = [FileClass, 'emergencyPlanFileDto', 1]
const STEP = [StepClass, 'emergencyPlanStepDto', 1]

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询应急预案列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM, BASE)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryEmergencyPlanList', DTO)
    return [resp.comQuery.totalCount, resp.emergencyPlanListDto || []]
  },
  transList(arr) {
    return arr.map(({ emergencyPlanDto, ...other }) => ({ ...emergencyPlanDto, ...other }))
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ id }, TIP = '查询应急预案详情') {
    const DTO = CORE.grpcDTO(OneClass, { id })
    const resp = await CORE.grpc(TIP, CLIENT, 'queryEmergencyPlanDetail', DTO)
    return resp.emergencyPlanDetailDto || {}
  },
  transOne({ emergencyPlanDto, ...other }) {
    const groupId = other.emergencyPlanGroupDto.map((item) => item.emergencyGroupId)
    const one = { ...emergencyPlanDto, ...other, groupId }
    other.emergencyPlanStepDto.forEach((item) => {
      one[`child_${item.id}`] = item.stepDesc ? 'ready' : ''
    })
    return one
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  transAdd(one) {
    one.emergencyPlanStepDto.forEach((item, i) => (item.stepNo = i + 1))
    return one
  },
  async add(one, TIP = '新增应急预案') {
    const DTO = CORE.grpcDTO(AddClass, one, BASE, GROUP, FILE, STEP)
    await CORE.grpc(TIP, CLIENT, 'insertEmergencyPlan', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  transEditToAdd: true,
  async edit(one, TIP = '修改应急预案') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE, GROUP, FILE, STEP)
    await CORE.grpc(TIP, CLIENT, 'updateEmergencyPlan', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除应急预案') {
    const DTO = CORE.grpcDTO(DelClass, { ids: id })
    await CORE.grpc(TIP, CLIENT, 'deleteEmergencyPlan', DTO)
    return CORE.grpcOK(TIP)
  },
}
